package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.text.TextUtils;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.moudle.mine.contract.ConfirmRechargeContract;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Function4;
import io.reactivex.functions.Function5;
import io.reactivex.functions.Function6;
import io.reactivex.schedulers.Schedulers;

public class ConfirmRechargePresenter implements ConfirmRechargeContract.Presenter {

    private ConfirmRechargeContract.View mContractView;

    private CompositeDisposable disposable;

    public ConfirmRechargePresenter(ConfirmRechargeContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void submit(String productid,String orderid,String recharge_username,String recharge_num,String recharge_date,String total_fee,String pay_total_fee,String touch_us,String to_username,String to_num) {
        Request.getIntence().confirmRecharge(StorageHelper.getToken(), productid, orderid, recharge_username, recharge_num, recharge_date, total_fee, pay_total_fee,touch_us, to_username, to_num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setSumitResults(baseResponse);
                        }
                    }
                });

    }

    @Override
    public void getConfirmCanClick(TextView tv1, TextView tv2, TextView tv4, TextView tv5) {
        Observable.combineLatest(isEditTextInputTextObservable(tv1),
                isEditTextInputTextObservable(tv2),
                isEditTextInputTextObservable(tv4),
                isEditTextInputTextObservable(tv5),
                new Function4<Boolean, Boolean, Boolean, Boolean, Boolean>() {
                    @Override
                    public Boolean apply(Boolean aBoolean, Boolean aBoolean2, Boolean aBoolean3, Boolean aBoolean4) throws Exception {
                        return aBoolean&&aBoolean2&&aBoolean3&&aBoolean4;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(mContractView != null){
                            if(aBoolean){
                                mContractView.setConfirmCanClick(R.color.trans_blue,R.color.base_blue,true);
                            }else{
                                mContractView.setConfirmCanClick(R.color.text_normal_gary,R.color.base_white,false);
                            }

                        }
                    }
                });
    }

    private Observable<Boolean> isEditTextInputTextObservable(TextView tv) {
        return RxTextView.afterTextChangeEvents(tv)
                .map(new Function<TextViewAfterTextChangeEvent, Boolean>() {
                    @Override
                    public Boolean apply(TextViewAfterTextChangeEvent event) throws Exception {
                        return !TextUtils.isEmpty(event.view().getText());
                    }
                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
